﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace hmc.im.client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private static byte[] result = new byte[1024];
        Socket clientSocket = null;
        public MainWindow()
        {
            InitializeComponent();


        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (clientSocket != null && clientSocket.Connected) clientSocket.Close();
            }
            catch { }
            base.OnClosing(e);
        }

        private int GetOrderNotify()
        {
            return 1;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            //设定服务器IP地址  
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8885)); //配置服务器IP与端口  
                Console.WriteLine("连接服务器成功");
            }
            catch
            {
                Console.WriteLine("连接服务器失败，请按回车键退出！");
                return;
            }

            while (true)
            {
                //通过clientSocket接收数据  
                int receiveLength = clientSocket.Receive(result);
                if (receiveLength > 0)
                {


                    ShowWindow(Encoding.ASCII.GetString(result, 0, receiveLength));
                    //Console.WriteLine("接收服务器消息：{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
                }
                else
                {
                    MessageBox.Show("没有新订单");
                }
            }
            
        }

        void ShowWindow(string content)
        {
            //MessageWindow 是你自定义的窗口消息框，width和height一定要赋值
            //Window msgWindow = new Window() { Width = 300, Height = 300 };

            notify msgWindow = new notify(content) { Width = 300, Height = 300 };
            msgWindow.WindowStartupLocation = WindowStartupLocation.Manual;
            msgWindow.ShowInTaskbar = false;

            msgWindow.Left = SystemParameters.WorkArea.Width - msgWindow.Width;
            msgWindow.Top = SystemParameters.WorkArea.Height - msgWindow.Height;
            
            msgWindow.Show();
            Thread.Sleep(5000);
            msgWindow.Close();
            MessageBox.Show(msgWindow, content);
        }



    }
}
